# Contributor: Holger Jaekel <holger.jaekel@gmx.de>
# Maintainer: Holger Jaekel <holger.jaekel@gmx.de>
pkgname=dcmtk
pkgver=3.6.8
_pkgver=${pkgver//./}
pkgrel=0
pkgdesc="OFFIS DICOM toolkit"
url="https://dcmtk.org/"
# armhf: compile fails with bad immediate value for offset
arch="all !armhf"
license="BSD-3-Clause"
makedepends="
	cmake
	libpng-dev
	libsndfile-dev
	libxml2-dev
	openjpeg-dev
	openssl-dev
	samurai
	tiff-dev
	zlib-dev
	"
install="$pkgname.pre-install"
subpackages="$pkgname-dev $pkgname-doc $pkgname-openrc libdcmtk"
source="https://dicom.offis.de/download/dcmtk/dcmtk$_pkgver/dcmtk-$pkgver.tar.gz
	10-offile.patch
	20-thai.patch
	30-dont_export_all_executables.patch
	40-dcmqrscp.patch
	dcmtk.initd
	"

# secfixes:
#   3.6.7-r0:
#     - CVE-2022-43272

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		local crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	# SKIP_RPATH=OFF to run test suite
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=ON \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DBUILD_SHARED_LIBS=ON \
		-DDCMTK_WITH_OPENSSL=ON \
		-DDCMTK_ENABLE_PRIVATE_TAGS=ON \
		-DDCMTK_WITH_XML=ON \
		-DDCMTK_WITH_DOXYGEN=OFF \
		-DDCMTK_WITH_TIFF=ON \
		-DDCMTK_WITH_ZLIB=ON \
		-DDCMTK_WITH_ICONV=OFF \
		-DDCMTK_WITH_ICU=OFF \
		-DUSE_COMPILER_HIDDEN_VISIBILITY=ON \
		-DCMAKE_SKIP_RPATH=OFF \
		-DDCMTK_ENABLE_STL=ON \
		$crossopts
	cmake --build build
}

check() {
	ctest --test-dir build --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# remove test binaries
	rm "$pkgdir"/usr/bin/*_tests
	rm "$pkgdir"/usr/bin/drttest
	rm "$pkgdir"/usr/bin/mkreport
	rm "$pkgdir"/usr/bin/msgserv
	rm "$pkgdir"/usr/bin/wltest
	rm "$pkgdir"/usr/share/dcmtk-$pkgver/SC.dump
	rm "$pkgdir"/usr/share/dcmtk-$pkgver/VLP.dump

	# remove changelog
	rm -rf "$pkgdir"/usr/share/doc

	# remove example modality worklist database and example queries
	rm -rf "$pkgdir"/usr/share/dcmtk/wlistdb "$pkgdir"/usr/share/dcmtk/wlistqry
}

openrc() {
	pkgdesc="$pkgdesc (OpenRC init scripts)"
	install -Dm755 "$srcdir"/dcmtk.initd "$subpkgdir"/etc/init.d/dcmtk
}

libdcmtk() {
	pkgdesc="OFFIS DICOM toolkit runtime libraries"
	amove usr/lib
	amove usr/share/dcmtk-$pkgver/*.dic
}

sha512sums="
1bb2aad1aa63e0a1b79d92d7c932a969deccde03fdb484dbd44198effb58d50de44a2b0cda150ce7df63e4e986af5bc1f694c8a7988b4c049c578d83ba81184a  dcmtk-3.6.8.tar.gz
14f9faa64c04435345cb46511f5d4e1c17fc000df2ddca330d28961b92a16b06dd9b689d331583009ac2d34e307d28c5bcaec0704f70329328b4100fc9efd042  10-offile.patch
70dd062b3e0b176c6a3627ea6b30383d7c21ea4088c2f76ece8123a6d625de482c05cf9ad4f76a290a5d3308616d0ba2fc34189c337f7c475aadb8ff1c9c101b  20-thai.patch
1a487ec720d75945a6a62a8e6b5f5dc33a363af1f58595ac84fc910b76dadd458f1c6b599bdea6e45570630496ce8d7c5efc544252f6421d83c7370d83e9b8b1  30-dont_export_all_executables.patch
37b7867b379456aed71f15609dd57be142984b0430605eba9771d2766817ac95e6526759b105686a76df28f09e49ee5e8907f46c38b4d7de947278b0faaf7568  40-dcmqrscp.patch
dd8e24011178f3b5fa35150439aea2640a6f9da702da883a61e6eeb835223ee6967e4586f4e75b004e04da0f061c1edbf6d00a252480c64142a68b3213810aad  dcmtk.initd
"
